package cn.bocai.pc.ws.service.impl;

import cn.bocai.pc.FileMonitorMethod.FileListener;
import cn.bocai.pc.FileMonitorMethod.FileMonitor;
import cn.bocai.pc.FileMonitorMethod.FileObserver;
import cn.bocai.pc.ws.service.MonitorFileService;

public class MonitorFileServiceImpl implements MonitorFileService{
	//D:/ruanjian/eclipse/file/newBcai/pc-image/src/main/webapp/2015
	 FileObserver ob;
	 FileMonitor monitor =null;
	 int  monitorState=0;
	@Override
	public String startScan(String Path,String opinion) {
		//保证线程的唯一性和区别启动和结束，所以这需要加判断
		String data="none";
		if("start".equals(opinion)){
			ob = new FileObserver(Path);
			testStrt();
			monitorState=1;
			data="start";
		}
		if("stop".equals(opinion)){
			testStop();
			monitorState=0;
			data="stop";
		}
		if("psgeStart".equals(opinion)){
			if(0==monitorState){//当第一个进入的时候开启，以后就不能再开启了，除非运行了关闭，才可以
				data="not";
			}else{
				data="start";
			}
		}
		return data;
	}
	//启动监测
	 public void testStrt() {
		 FileListener listener = new FileListener();  
	     ob.addListener(listener);  
	     monitor = new FileMonitor(ob);
	     monitor.start();
	     System.out.println("文件夹监测启动");
	 }
	//结束监测 
	 public void testStop() {
		 monitor.stop();
		 System.out.println("文件夹监测结束");
	 }
	 
	 /** 
	    *  
	    * @param dbdir mysql数据库安装路径 
	    * @param dbname  数据库的名称 
	    * @param backdir 备份的目录 
	  */ 
	/*@Override
	public String backupsDate(String dbdir, String dbname, String backdir) {
		 Calendar calendar = Calendar.getInstance();  
	        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HHmmss");  
	        String currentTime = dateFormat.format(calendar.getTime()); 
	        String 	returndate;
	        try {  
	            long startTime = System.currentTimeMillis();  
	            Runtime rt = Runtime.getRuntime();  
	            Process child = rt  
	                    .exec(dbdir + "/bin/mysqldump --default-character-set=utf8 -uroot -proot "+ dbname);  
	            InputStream in = child.getInputStream();  
	            InputStreamReader xx = new InputStreamReader(in, "utf8");  
	  
	            FileOutputStream fout = new FileOutputStream(new File(backdir, dbname + "_" + currentTime + ".sql"));  
	            OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");  
	  
	            dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	  
	            writer.write("-- Dump by Microsoul at " + dateFormat.format(calendar.getTime()) + "\r\n");  
	  
	            String inStr;  
	            BufferedReader br = new BufferedReader(xx);  
	            // 这样实时写入文件很重要，网上有很多是将读取的存入字符串中，最后再写成文件，这样会导致Java的堆栈内存溢出。  
	            while ((inStr = br.readLine()) != null) {  
	                writer.write(inStr);  
	                writer.write("\r\n");  
	            }  
	  
	            writer.write("\r\n-- Use " + (System.currentTimeMillis() - startTime) + "ms\r\n");  
	  
	            writer.flush();  
	            in.close();  
	            xx.close();  
	            br.close();  
	            writer.close();  
	            fout.close();  
	            returndate="succeed";
	        } catch (Exception e) {  
	            PrintStream print = null;  
	            try {  
	                print = new PrintStream(new File(backdir, currentTime + "_backup_err.log"));  
	                dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");  
	                currentTime = dateFormat.format(calendar.getTime());  
	                print.println(currentTime + "  backup failed.");  
	                e.printStackTrace(print);  
	                print.flush();  
	            } catch (IOException e2) {  
	  
	            } finally {  
	                if (print != null) {  
	                    print.close();  
	                }  
	            }  
	            returndate="erro";
	        } 
		return returndate;
	}*/
	 
}
